package com.zyx.javademo.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyx.javademo.bean.Dog;
import com.zyx.javademo.bean.JsonFieldDemo;
import com.zyx.javademo.bean.User;

import java.math.BigDecimal;

/**
 * @author zyx
 * @since 2021/04/26 22:45
 */
public class JsonDemo {
    public static void main(String[] args) {
        jsonTransTest();
    }

    private static void jsonTransTest() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "zhangsan");
        jsonObject.put("age", "35");
        jsonObject.put("height", "180.0000");
        User user = JSONObject.parseObject(jsonObject.toJSONString(), User.class);
        System.out.println(user);
    }

    private static void jsonFieldTest() {
        JsonFieldDemo jfOri = new JsonFieldDemo();
        jfOri.setP("1028");

        String jfStr = JSON.toJSONString(jfOri);
        System.out.println("jf str => " + jfStr);

        JSONObject jsonObj = JSONObject.parseObject(jfStr);
        jsonObj.remove("price");

        System.out.println("json obj p => " + jsonObj.get("p"));
        System.out.println("json obj price => " + jsonObj.get("price"));


        JsonFieldDemo jf = JSONObject.parseObject(jsonObj.toJSONString(), JsonFieldDemo.class);
        System.out.println("jf p => " + jf.getP());

        /*JsonFieldDemo jf = JSONObject.parseObject(JSON.toJSONString(new JsonFieldDemo()), JsonFieldDemo.class);
        String p = jf.getP();
        if (p == null) {
            System.out.println("null");
        } else {
            System.out.println("p is not null");
        }*/
    }

    public static void jsonObjectTest1() {
        User user1 = new User("zhangsan", 19, new BigDecimal(180));
        User user2 = new User("lisi", 19, new BigDecimal(180));
        String str1 = JSONObject.toJSONString(user1);
        String str2 = JSONObject.toJSONString(user2);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(user1);
        jsonArray.add(user2);
        /*jsonArray.add(str1);
        jsonArray.add(str2);*/
        for (int i = 0; i < jsonArray.size(); i++) {
            System.out.println(jsonArray.getJSONObject(i).toJSONString());
        }
    }

    public static void jsonObjectTest2() {
        String jsonStr = "{\n" +
                "\t\"data\": {\n" +
                "\t\t\"induName\": \"医疗保健设备与服务\",\n" +
                "\t\t\"tops\": [{\n" +
                "\t\t\t\"eventTime\": \"1621475174744\",\n" +
                "\t\t\t\"stockCode\": \"00853\",\n" +
                "\t\t\t\"stockName\": \"微创医疗\",\n" +
                "\t\t\t\"totalMarketValue\": \"10591803.47\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"-0.09\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621474801787\",\n" +
                "\t\t\t\"stockCode\": \"02120\",\n" +
                "\t\t\t\"stockName\": \"康宁医院\",\n" +
                "\t\t\t\"totalMarketValue\": \"206269.83\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+0.00\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475163335\",\n" +
                "\t\t\t\"stockCode\": \"01858\",\n" +
                "\t\t\t\"stockName\": \"春立医疗\",\n" +
                "\t\t\t\"totalMarketValue\": \"944175.96\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+0.18\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475171057\",\n" +
                "\t\t\t\"stockCode\": \"01066\",\n" +
                "\t\t\t\"stockName\": \"威高股份\",\n" +
                "\t\t\t\"totalMarketValue\": \"8113064.19\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"-1.86\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621473005615\",\n" +
                "\t\t\t\"stockCode\": \"03869\",\n" +
                "\t\t\t\"stockName\": \"弘和仁爱医疗\",\n" +
                "\t\t\t\"totalMarketValue\": \"160305.04\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+0.00\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475132245\",\n" +
                "\t\t\t\"stockCode\": \"02138\",\n" +
                "\t\t\t\"stockName\": \"医思健康\",\n" +
                "\t\t\t\"totalMarketValue\": \"1226556.44\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"-1.25\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475014144\",\n" +
                "\t\t\t\"stockCode\": \"06118\",\n" +
                "\t\t\t\"stockName\": \"奥星生命科技\",\n" +
                "\t\t\t\"totalMarketValue\": \"480289.33\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+0.11\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475120315\",\n" +
                "\t\t\t\"stockCode\": \"01515\",\n" +
                "\t\t\t\"stockName\": \"华润医疗\",\n" +
                "\t\t\t\"totalMarketValue\": \"920640.33\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"-0.28\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475172304\",\n" +
                "\t\t\t\"stockCode\": \"02666\",\n" +
                "\t\t\t\"stockName\": \"环球医疗\",\n" +
                "\t\t\t\"totalMarketValue\": \"1286246.97\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+0.74\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"eventTime\": \"1621475174375\",\n" +
                "\t\t\t\"stockCode\": \"03600\",\n" +
                "\t\t\t\"stockName\": \"现代牙科\",\n" +
                "\t\t\t\"totalMarketValue\": \"518171.58\",\n" +
                "\t\t\t\"tradeDate\": \"2021-05-20\",\n" +
                "\t\t\t\"upAndDown\": \"+4.25\"\n" +
                "\t\t}],\n" +
                "\t\t\"induCode\": \"0501\",\n" +
                "\t\t\"timestamp\": \"1621475165000\"\n" +
                "\t},\n" +
                "\t\"type\": \"L2-TOP-10\"\n" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        long ts = Long.parseLong(jsonObject.getJSONObject("data").getString("timestamp"));
        System.out.println(ts);
    }



    private static void jsonArrayTest() {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name", "zhangsan");
        jsonObject1.put("sex", "man");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name", "zhangsan");
        jsonObject2.put("sex", "woman");
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        System.out.println(jsonArray.toJSONString());
    }


}
